cmake_minimum_required(VERSION 3.10)

# 设置项目名称
project(HighConcurrencyServer)

# 设置 C++ 标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# 仅在生成可执行文件时，将其放到 /bin
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

# 添加主可执行文件
# add_executable(main src/main.cpp src/timerfd.cc)
# add_executable(timerfd example/timerfd.cc)
# set_target_properties(timerfd
#     PROPERTIES
#         RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
#         OUTPUT_NAME my_timerfd
# )
add_executable(regex example/regex.cc)
set_target_properties(regex
    PROPERTIES
        RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
        OUTPUT_NAME my_regex
)

add_executable(main Http/main.cc src/TcpServer.cpp)
set_target_properties(main
    PROPERTIES
        RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
        OUTPUT_NAME my_main
)
target_link_libraries(main
    server_lib
    pthread
)

#检测是否存在内存泄漏
####set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=address,leak")
####set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address,leak")

# 将 src 目录下的源文件编译成一个库
# 每次src更新后都需要使用cmake -G Ninja ..重新编译，直接ninja构建的话会报连接错误
aux_source_directory(src SERVER_SOURCES)
add_library(server_lib ${SERVER_SOURCES})
target_include_directories(server_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)

# 添加测试可执行文件
#[[cmake的块注释语法]]
#[[
add_executable(test_buffer test/test_buffer.cpp)
set_target_properties(test_buffer
    PROPERTIES
        RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
        OUTPUT_NAME test_buffer
)
target_link_libraries(test_buffer server_lib)


# 添加 socket 测试可执行文件
add_executable(test_socket test/test_socket.cpp)
set_target_properties(test_socket
    PROPERTIES
        RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
        OUTPUT_NAME test_socket
)
target_link_libraries(test_socket server_lib)
]]



# 添加新的服务端测试程序
add_executable(server_test EchoServer/main.cc)
set_target_properties(server_test PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
target_link_libraries(server_test server_lib pthread)


# 添加新的客户端测试程序
add_executable(client_test test/client.cc Http/Http.hpp)
set_target_properties(client_test PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
target_link_libraries(client_test server_lib pthread)
